﻿using System;

namespace Ex1ComplexNumber
{
    class ComplexNumber
    {
        private decimal real;
        private decimal imaginery;

        public decimal Real
        {
            get
            {
                return real;
            }
            set
            {
                real = value;
            }
        }

        public decimal Imaginery
        {
            get
            {
                return imaginery;
            }
            set
            {
                imaginery = value;
            }
        }

        public ComplexNumber(decimal real, decimal imaginery)
        {
            this.real = real;
            this.imaginery = imaginery;
        }

        public static ComplexNumber operator +(ComplexNumber num1, ComplexNumber num2)
        {
            decimal realResult = num1.Real + num2.Real;
            decimal imgResult = num1.Imaginery + num2.Imaginery;
            ComplexNumber result = new ComplexNumber(realResult, imgResult);
            return result;
        }

        public static ComplexNumber operator -(ComplexNumber num1, ComplexNumber num2)
        {
            decimal realResult = num1.Real - num2.Real;
            decimal imgResult = num1.Imaginery - num2.Imaginery;
            ComplexNumber result = new ComplexNumber(realResult, imgResult);
            return result;
        }

        public static ComplexNumber operator *(ComplexNumber num1, ComplexNumber num2)
        {
            decimal realResult = num1.Real * num2.Real - num1.Imaginery*num2.Imaginery;
            decimal imgResult = num1.Imaginery * num2.Real + num1.Real * num2.Imaginery;
            ComplexNumber result = new ComplexNumber(realResult, imgResult);
            return result;
        }

        public static ComplexNumber operator /(ComplexNumber num1, ComplexNumber num2)
        {
            decimal x = (num1.Real * num2.Real + num1.Imaginery * num2.Imaginery) /
                (num2.Real * num2.real + num2.Imaginery * num2.Imaginery);
            decimal y = (num1.Imaginery * num2.real - num1.real * num2.Imaginery) / 
                (num2.Real * num2.real + num2.Imaginery * num2.Imaginery);
            return new ComplexNumber(x, y);
        }

        public static implicit operator ComplexNumber(double num)
        {
            return new ComplexNumber((decimal)num,0);
        }

        public static explicit operator double(ComplexNumber num)
        {
            return (double)num.Real;
        }

        public override string ToString()
        {
            return string.Format("{0} + i {1}",this.Real,this.Imaginery);
        }

        public decimal this[int i]
        {
            get
            {
                if (i==0)
                {
                    return this.Real;
                }
                if (i==1)
                {
                    return this.Imaginery;
                }
                throw new InvalidOperationException("The index is invalid!");
            }
        }
    }
}
